/**
 * @description: 「作品列表」组件
 * @author: Ruan Jiazhen
 * @date: 2024-03-13 18:32:09
 **/
import { reqLikeWork } from '@/api/hosting-style';
import { ComponentWithComputed } from 'miniprogram-computed';

// XXX 暂时使用这样的固定高度伪装成瀑布流布局，后续如果有需求再想办法改
const WORK_ITEM_HEIGHT_LIST_1 = [170, 240, 300, 400, 250, 180, 260, 200, 280, 220];
const WORK_ITEM_HEIGHT_LIST_2 = [220, 280, 200, 260, 180, 250, 400, 300, 240, 170];
ComponentWithComputed({
  properties: {
    workList: {
      type: Array,
      value: [],
    },
    avatar: {
      type: String,
      value: '',
    },
    name: {
      type: String,
      value: '',
    },
  },
  computed: {
    workListWithHeight(properties: typeof this.properties) {
      return [
        properties.workList
          .filter((_: Work, index: number) => index % 2 === 0)
          .map((item: Work, index: number) => ({
            ...item,
            height: WORK_ITEM_HEIGHT_LIST_1[index % WORK_ITEM_HEIGHT_LIST_1.length],
          })),
        properties.workList
          .filter((_: Work, index: number) => index % 2 === 1)
          .map((item: Work, index: number) => ({
            ...item,
            height: WORK_ITEM_HEIGHT_LIST_2[index % WORK_ITEM_HEIGHT_LIST_2.length],
          })),
      ];
    },
  },
  methods: {
    async like(e: WechatMiniprogram.Touch) {
      try {
        const { find_id, isLike } = e.currentTarget.dataset;
        if (isLike) {
          throw new Error('已经点过赞了');
        }
        const res = await reqLikeWork(find_id);
        console.log('res', res);
        if (res.code === 200) {
          wx.showToast({
            title: '点赞成功',
            icon: 'success',
          });
          this.triggerEvent('onLike', { find_id });
          return;
        }

        throw new Error(res.msg);
      } catch (error: any) {
        wx.showToast({
          title: error.message,
          icon: 'none',
        });
      }
    },
    onClickWorkItem(e: WechatMiniprogram.Touch) {
      const { find_id } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/subpkg/pages/work-detail/work-detail?find_id=${find_id}`,
      });
    },
    onClickAdd(){
      this.triggerEvent('onClickAdd');
    }
  },
});
